using UnityEngine;
using UnityEngine.Audio;

public class collisionSound : MonoBehaviour
{
	public AudioMixer masterMixer;

	public AudioClip[] middleSound;

	public AudioClip[] greatSound;

	private AudioSource colAudio;

	public float middleDamage = 20f;

	public float greatDamage = 100f;

	private float lastTime;

	private float interval = 0.1f;

	private float intervalRand = 0.4f;

	private float impactSpeed;

	private Vector3 sumImpactVelocity;

	private Vector3 localImpactVelocity;

	private void Start()
	{
		colAudio = base.gameObject.AddComponent<AudioSource>();
		colAudio.loop = false;
		colAudio.clip = middleSound[Random.Range(0, middleSound.Length)];
		colAudio.volume = 1f;
		colAudio.spatialBlend = 0.8f;
	}

	private void Update()
	{
		colAudio.outputAudioMixerGroup = masterMixer.FindMatchingGroups("collisions")[0];
		if (Time.time - lastTime >= interval)
		{
			localImpactVelocity = sumImpactVelocity;
			sumImpactVelocity = Vector3.zero;
			lastTime = Time.time + interval * UnityEngine.Random.Range(0f - intervalRand, intervalRand);
		}
		else
		{
			localImpactVelocity = Vector3.zero;
		}
		impactSpeed = localImpactVelocity.magnitude;
		if (impactSpeed > middleDamage && impactSpeed < greatDamage)
		{
			colAudio.volume = 0.4f + (impactSpeed - middleDamage) / (greatDamage - middleDamage);
			colAudio.clip = middleSound[Random.Range(0, middleSound.Length)];
			colAudio.Play();
		}
		else if (impactSpeed > greatDamage)
		{
			colAudio.volume = 1f;
			colAudio.clip = greatSound[Random.Range(0, greatSound.Length)];
			colAudio.Play();
		}
	}

	private void OnCollisionEnter(Collision collision)
	{
		ProcessContact(collision);
	}

	private void ProcessContact(Collision col)
	{
		Vector3 vector = Vector3.zero;
		ContactPoint[] contacts = col.contacts;
		for (int i = 0; i < contacts.Length; i++)
		{
			ContactPoint contactPoint = contacts[i];
			Collider otherCollider = contactPoint.otherCollider;
			Collider thisCollider = contactPoint.thisCollider;
			if ((otherCollider == null || otherCollider.attachedRigidbody != GetComponent<Rigidbody>()) && otherCollider.GetType() != typeof(WheelCollider) && thisCollider.GetType() != typeof(WheelCollider))
			{
				vector += col.relativeVelocity;
				sumImpactVelocity += base.transform.InverseTransformDirection(vector);
			}
		}
	}
}
